//
//  CloudPhotoPermissionVC.swift
//  YPWatermarkCamera
//
//  Created by 马涛 on 2022/7/7.
//

import UIKit
import WmcLogin
import YPBedrock

// MARK: - [WmcCloudPhotoPermissionVC]

class WmcGuideVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        showAlert()
    }

    // Internal

    static let wmc_base_screen_ratio = CGFloat(375.0 / 812.0)
    static let wmc_current_screen_ratio = Wmc_screenWidth / Wmc_screenHeight
    static let wmc_ratio = wmc_base_screen_ratio / wmc_current_screen_ratio

    lazy var topRightImgView: UIImageView = {
        let view = UIImageView()
        view.image = "guide_bg_top".wmc_image
        view.contentMode = .scaleAspectFit
        return view
    }()

    lazy var topTextImgView: UILabel = {
        let view = UILabel()
        let paragraph = NSMutableParagraphStyle()
        paragraph.lineSpacing = 10
        let attriStr = "真实时间地点防篡改\n照片自动分类归档\n无限云端存储空间，永久免费保存".yp.makeAttributed { make in
            make.set(font: 18.yp.uiFont(weight: .regular))
                .set(foregroundColor:UIColor.wmc_mainTheme)
                .set(paragraphStyle:paragraph)
        }
        view.attributedText = attriStr
        view.numberOfLines = 0
        return view
    }()

    lazy var contentImgView: UIImageView = {
        let view = UIImageView()
        view.image = "guide_bg_Img".wmc_image
        view.contentMode = .scaleAspectFit
        return view
    }()

    lazy var bottomLeftImgView: UIImageView = {
        let view = UIImageView()
        view.image = "guide_bg_bottom".wmc_image
        view.contentMode = .scaleAspectFit
        return view
    }()

    lazy var instantExperienceBtn: UIButton = {
        let view = UIButton()
        view.setTitle("立即体验", for: .normal)
        view.setTitleColor(UIColor.wmc_white_FFFFFF, for: .normal)
        view.backgroundColor = UIColor.wmc_mainTheme
        view.layer.cornerRadius = 3
        view.clipsToBounds = true
        view.titleLabel?.font = 16.yp.uiFont(weight: .medium)
        return view
    }()

    override func bindViewAction() {
        instantExperienceBtn.yp.addEvent { _ in
            guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
            // 切换窗口
            WmcUserdefault.save(value: false, forKey: .State.Guide.isShowGuide) // 标记不需要
            appDelegate.window?.rootViewController = WmcNavVC(rootViewController: WmcHomeVC())
        }
    }

    override func makeUI() {
        addSubview(topRightImgView)
        addSubview(topTextImgView)
        addSubview(contentImgView)
        addSubview(bottomLeftImgView)
        addSubview(instantExperienceBtn)

        topRightImgView.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.width.equalTo(80 * WmcGuideVC.wmc_ratio)
            make.height.equalTo(142 * WmcGuideVC.wmc_ratio)
        }
        let width = (Wmc_screenWidth - 69 * WmcGuideVC.wmc_ratio)
        topTextImgView.snp.makeConstraints { make in
            make.top.equalTo(74 * WmcGuideVC.wmc_ratio)
            make.left.equalTo(39 * WmcGuideVC.wmc_ratio)
            make.right.equalTo(-30 * WmcGuideVC.wmc_ratio)
            make.height.equalTo(width * 91 / 306.0)
        }
        contentImgView.snp.makeConstraints { make in
            make.top.equalTo(topTextImgView.snp.bottom).offset(41 * WmcGuideVC.wmc_ratio)
            make.left.equalTo(45 * WmcGuideVC.wmc_ratio)
            make.right.equalTo(-45 * WmcGuideVC.wmc_ratio)
        }
        bottomLeftImgView.snp.makeConstraints { make in
            make.left.bottom.equalToSuperview()
            make.width.equalTo(87 * WmcGuideVC.wmc_ratio)
            make.height.equalTo(132 * WmcGuideVC.wmc_ratio)
        }
        instantExperienceBtn.snp.makeConstraints { make in
            make.top.equalTo(contentImgView.snp.bottom).offset(52 * WmcGuideVC.wmc_ratio)
            make.left.equalTo(37 * WmcGuideVC.wmc_ratio)
            make.right.equalTo(-37 * WmcGuideVC.wmc_ratio)
            make.height.equalTo(44)
            make.bottom.equalTo(-56 * WmcGuideVC.wmc_ratio)
        }
    }

    func showAlert() {
        let content =
            """
            欢迎使用鱼泡水印相机，请您充分阅读并理解《用户协议》和《隐私政策》
            1.为向您提供拍照后保存到相册等功能，在获得您的明示同意后，我们会申请您设备的摄像头和存储空间权限，拒绝授权某些权限不影响您正常使用鱼泡水印相机其他功能；
            2.为在照片上显示位置相关水印，我们会申请您的位置权限；
            3.为了生成唯一推送目标ID为您提供更稳定的推送服务和体验，我们接入的SDK会申请获取您设备信息（IMEI和mac地址）和应用列表信息，SDK列表见《隐私政策》完整版。
            4.我们会采用业界领先的安全技术保护好您的个人信息。
            """
        let attContent = content.yp.makeAttributed { make in
            // 标记重点
            let markRanges = content.yp.searchRanges(with: [
                "《用户协议》",
                "《隐私政策》",
            ])
            for (idx, range) in markRanges.enumerated() {
                make.set(font: 16)
                make.set(foregroundColor:UIColor.wmc_grayH8_8A8A99)
                make.set(foregroundColor:UIColor.wmc_mainTheme, range: range)
                // 区分两者，但是时间问题此处使用劣等方案
                if idx == 0 {
                    make.set(link:WmcCameraSettingVM.ServiceAgreement.url, range: range)
                } else {
                    make.set(link:WmcCameraSettingVM.PrivacyAgreement.url, range: range)
                }
            }
            // 设置段落
            let paragraphStyle = NSMutableParagraphStyle().yp.makeParagraphStyle { make in
                make.set(minimumLineHeight:25)
            }
            make.set(paragraphStyle:paragraphStyle)
        }

        let alert = WmcAlertView()
        alert.contentType = .textView
        alert.title = "用户协议和隐私政策概要"
        alert.attributeMsg = attContent
        alert.contentLabel.textAlignment = .left
        alert.style = .custom(confirmTitle: "同意", cancelTitle: "不同意")
        alert.confirmBlock = { }
        alert.cancelBlock = {
            exit(0) // 闪退
        }
        alert.linkDidSelectBlock = { link in
            let vm = WmcWebVM()
            vm.urlString = link
            switch link {
            case WmcCameraSettingVM.ServiceAgreement.urlStr:
                vm.title = WmcCameraSettingVM.ServiceAgreement.name
            case WmcCameraSettingVM.PrivacyAgreement.urlStr:
                vm.title = WmcCameraSettingVM.PrivacyAgreement.name
            default: break
            }
            WmcNavigatorManager.present(.web, parms: vm)
            return false
        }
        alert.show(height: 500)
    }
}
